package com.example.demo.nuoqi.service.impl;

import com.example.demo.nuoqi.dao.ProfessionMapper;
import com.example.demo.nuoqi.pojo.Profession;
import com.example.demo.nuoqi.service.ProfessionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Profession业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class ProfessionServiceImpl implements ProfessionService {

    @Autowired
    private ProfessionMapper professionMapper;


    /**
     * Profession条件+分页查询
     * @param profession 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Profession> findPage(Profession profession, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(profession);
        //执行搜索
        return new PageInfo<Profession>(professionMapper.selectByExample(example));
    }

    /**
     * Profession分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Profession> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Profession>(professionMapper.selectAll());
    }

    /**
     * Profession条件查询
     * @param profession
     * @return
     */
    @Override
    public List<Profession> findList(Profession profession){
        //构建查询条件
        Example example = createExample(profession);
        //根据构建的条件查询数据
        return professionMapper.selectByExample(example);
    }


    /**
     * Profession构建查询对象
     * @param profession
     * @return
     */
    public Example createExample(Profession profession){
        Example example=new Example(Profession.class);
        Example.Criteria criteria = example.createCriteria();
        if(profession!=null){
            // 
            if(!StringUtils.isEmpty(profession.getId())){
                    criteria.andEqualTo("id",profession.getId());
            }
            // 
            if(!StringUtils.isEmpty(profession.getProfessionalTitle())){
                    criteria.andEqualTo("professionalTitle",profession.getProfessionalTitle());
            }
            // 
            if(!StringUtils.isEmpty(profession.getStatus())){
                    criteria.andEqualTo("status",profession.getStatus());
            }
            // 
            if(!StringUtils.isEmpty(profession.getCreateTime())){
                    criteria.andEqualTo("createTime",profession.getCreateTime());
            }
            // 
            if(!StringUtils.isEmpty(profession.getDirectionId())){
                    criteria.andEqualTo("directionId",profession.getDirectionId());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        professionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Profession
     * @param profession
     */
    @Override
    public void update(Profession profession){
        professionMapper.updateByPrimaryKey(profession);
    }

    /**
     * 增加Profession
     * @param profession
     */
    @Override
    public void add(Profession profession){
        professionMapper.insert(profession);
    }

    /**
     * 根据ID查询Profession
     * @param id
     * @return
     */
    @Override
    public Profession findById(Integer id){
        return  professionMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Profession全部数据
     * @return
     */
    @Override
    public List<Profession> findAll() {
        return professionMapper.selectAll();
    }
}
